//
//  NSXLeftShakeViewController.m
//  B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//
@import UIKit;
@import QuartzCore;
#import "NSXLeftShakeViewController.h"
#import "NSXLeftShakeModel.h"
#import "NSXLeftShakeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "FitHeader.h"
#import "XMHNetWorkongMethods.h"
#import "MBProgressHUD.h"
#import "NSXFirstPageWEBViewController.h"
#import "ImageCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "WillShakeView.h"
@interface NSXLeftShakeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic , retain)UITableView *tableView;
@property(nonatomic , assign)NSInteger page;
@property(nonatomic , retain)NSMutableArray *dataSourceArray;
@property(nonatomic , retain)NSTimer *timer;
@property(nonatomic , assign)NSInteger flag;
@property(nonatomic , retain)MBProgressHUD *progressView;
@property(nonatomic , retain)UIImageView *imagView;
@property(nonatomic , retain)UILabel *shakeLabel;
@property(nonatomic, strong)WillShakeView *willShakeView;
@end


@implementation NSXLeftShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}

#pragma mark 初始化tablev
-(void)loadView
{
    [super loadView];
    _page = arc4random() % 178;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    [self getData];
    [self createCollectionView];
    _willShakeView = [[WillShakeView alloc]initWithFrame:self.view.frame];
    [self.view addSubview:_willShakeView];
}
#pragma mark -创建collectionView
-(void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 1;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.itemSize = CGSizeMake(150 *FitWidth, 150 *FitHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * FitHeight, 10 * FitWidth, 10 * FitHeight, 10 * FitWidth);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:flowLayout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    [_collection registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"identifier2"];
}

#pragma mark - 解析数据
-(void)getData
{
    //    NSInteger page = arc4random()
    _dataSourceArray = [NSMutableArray array];
    NSString *string1 = @"http://ibaby.ipadown.com/api/food/food.show.list.php?category=%E7%83%98%E7%84%99&p=";
    NSString *string2 = @"&pagesize=12";
    NSString *urlString = [NSString stringWithFormat:@"%@%ld%@" , string1 , _page , string2];
    [XMHNetWorkongMethods getDataVyString:urlString BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"results"];
        for (NSDictionary *dic in array)
        {
            NSXFirstPageListModel *model = [[NSXFirstPageListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:model];
        }
        [_progressView removeFromSuperview];
        [_collection reloadData];
        NSLog(@"%@",_dataSourceArray);
        _tableView.userInteractionEnabled = YES;
    }];
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [_willShakeView removeFromSuperview];
    [_dataSourceArray removeAllObjects];
    [self getData];
    _page = arc4random() % 178;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    [cell.MyImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSourceArray[indexPath.row]thumb]]placeholderImage:[UIImage imageNamed:@"老鼠.png"]];
    cell.alpha = 0.2;
    [UIView animateWithDuration:1 animations:^{
        cell.alpha = 1;
    }];
    cell.titleLabel.text = [_dataSourceArray[indexPath.row]title];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionViewCell *cell = (ImageCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:.2 animations:^{
        cell.alpha = 0.5;
        cell.transform = CGAffineTransformMakeScale(1.4f, 1.4f);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:.2 animations:^{
            cell.alpha = 1.0;
            cell.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        } completion:^(BOOL finished) {
            NSXFirstPageWEBViewController *vc = [[NSXFirstPageWEBViewController alloc]init];
            vc.idString = [_dataSourceArray[indexPath.row]ID];
            vc.modelString = [_dataSourceArray[indexPath.row]title];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }];
}
#pragma mark 头视图的定义
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"identifier2" forIndexPath:indexPath];
    return header;
}
//头视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 30 * FitHeight);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((arc4random() % (170 - 150 + 1) + 150) * FitWidth, (arc4random() % 101 + 100 ) * FitHeight);
}
//修改行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return arc4random() % (10 - 5 + 1) + 5;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
